package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ47_字符串重新排序 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/47.txt"));
        String[] sArray = in.nextLine().split(" ");
        List<Node> res = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        Map<String, Integer> index = new HashMap<>();
        int k = 0;
        for (String s : sArray) {
            char[] sChar = s.toCharArray();
            Arrays.sort(sChar);
            String sAfter = String.valueOf(sChar);
            int count = map.getOrDefault(sAfter, 0) + 1;
            map.put(sAfter, count);
            if (count == 1) {
                res.add(new Node(sAfter, count, sAfter.length()));
                index.put(sAfter, k++);
            } else {
                res.get(index.get(sAfter)).count++;
            }
        }
        res.sort((a, b) -> {
            if (a.count == b.count) {
                if (a.length == b.length) {
                    return a.word.compareTo(b.word);
                } else {
                    return a.length - b.length;
                }
            } else {
                return b.count - a.count;
            }
        });
        for (Node a : res) {
            while (a.count-- > 0) {
                System.out.print(a.word + " ");
            }
        }
    }

    static class Node {
        String word;
        int count; //出现次数
        int length;

        public Node(String word, int count, int length) {
            this.word = word;
            this.count = count;
            this.length = length;
        }
    }
}
